package ltd.dujiabao.reserver.task;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;
import ltd.dujiabao.reserver.domain.SubmitParam;
import ltd.dujiabao.reserver.service.LoginService;
import ltd.dujiabao.reserver.service.ObjectStorageService;
import ltd.dujiabao.reserver.service.OcrService;
import ltd.dujiabao.reserver.service.SubmitService;
import ltd.dujiabao.reserver.service.UploadService;
import ltd.dujiabao.reserver.util.TimeUtils;

/**
 * @author : DuJiabao
 * @Project : reserver
 * @Time : 2021/11/7 15:36
 * @Desc :
 */
@Component
@Slf4j
public class ScheduledBookingTask {
    @Autowired
    private LoginService loginService;

    @Autowired
    private ObjectStorageService objectStorageService;

    @Autowired
    private OcrService ocrService;

    @Autowired
    private SubmitService submitService;

    @Autowired
    private UploadService uploadService;

    @Value("${reserver.retryTimes}")
    private Integer retryTimes;

    @Value("${reserver.cacheDir}")
    public String cacheDir;

    @Value("${reserver.login.cacheCodeFileName}")
    public String cacheCodeFileName;

    @Value("${reserver.upload.healthCodeFilePath}")
    public String healthCodeFilePath;

    @Value("${reserver.upload.routeCodeFilePath}")
    public String routeCodeFilePath;

    @Value("${reserver.submit.name}")
    public String submitName;

    @Value("${reserver.submit.mobile}")
    public String submitMobile;

    /**
     * 每周五、六早上执行。
     * 尝试n次，如果成功一次就结束，否则十分钟之后再来一次
     */
    @Scheduled(cron = "0 0 9 ? * 5-6")
    public void booking() {
        // 重置上传次数
        UploadService.resetUploadCount();
        for (int times = 0; times < retryTimes; times++, TimeUtils.sleepMin(10)) {
            log.info("Start booking service, already tried {} times", times);
            // 获取cookie
            String cookie = loginService.getCookie();
            String tempPath = cacheDir + cacheCodeFileName;
            // 下载验证码，并将验证码上传到对象存储之后，通过ocr服务识别出code
            if (!loginService.downloadValidationCode(cookie, tempPath)) {
                continue;
            }
            String url = objectStorageService.doUploadCodeFile(tempPath, cacheCodeFileName);
            String code = ocrService.doOcr(url);
            // 执行登录操作
            TimeUtils.sleepSecond(30);
            if (!loginService.doLogin(cookie, code)) {
                continue;
            }
            // 上传粤康码和行程码
            String healthFileName = uploadService.doUploadJpg(cookie, healthCodeFilePath);
            String routeFileName = uploadService.doUploadJpg(cookie, routeCodeFilePath);
            // 再次获取验证码，并识别
            if (!loginService.downloadValidationCode(cookie, tempPath)) {
                continue;
            }
            url = objectStorageService.doUploadCodeFile(tempPath, cacheCodeFileName);
            code = ocrService.doOcr(url);
            // 执行提交预约表单操作
            TimeUtils.sleepSecond(30);
            SubmitParam submitParam = new SubmitParam(submitName, submitMobile, healthFileName, routeFileName, code);
            if (submitService.doSubmit(cookie, submitParam)) {
                log.info("Finish {} time booking service, success.", times);
                return;
            }
            log.warn("Finish {} time booking service, but fail.", times);
        }
    }
}
